package com.org.zp.ctrl.login;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.org.base.ctrl.ActivityTaskManager;
import com.org.base.ctrl.BaseActivity;
import com.org.base.util.HttpUtils;
import com.org.base.util.URLHelper;
import com.org.zp.R;

/**
 * 登录
 * 
 * @author zhangb
 * 
 */
public class RegistActivity extends BaseActivity {

	private TextView headerTitle; // 顶部条标题
	private Button mButtonBack; // 顶部条返回按钮
	private Button mButtonName; // 顶部条右侧按钮

	private EditText mMail, mPhone, mPsw, mRePsw; // 邮箱名、密码

	private Button mLoginBt, mReg; // 登录、注册按钮
	private LinearLayout mComReg;
	private TextView reg_com_user_title;
	
	private String mClientType = "2";

	private ProgressDialog progressDialog;
	
	private String mType = "0";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reg);
		
		if (!(ActivityTaskManager.getInstance().containsName("RegistActivity"))) {
			ActivityTaskManager.getInstance().putActivity("RegistActivity", this);
		}
		
		mType = getIntent().getStringExtra("type");//intent.putExtra("type", "0");
		
		getComponentId();
	}

	public void getComponentId() {
		
		headerTitle = (TextView) findViewById(R.id.headTitle);
		headerTitle.setText("个人用户注册");
		
		mButtonBack = (Button) findViewById(R.id.back);
		mButtonBack.setText("  返回");
		mButtonBack.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityTaskManager.getInstance().removeActivity(
						"RegistActivity");
			}
		});
		
		mButtonName = (Button) findViewById(R.id.top_right_button);
		mButtonName.setVisibility(View.GONE);
		
		mMail = (EditText)findViewById(R.id.reg_mail);
		mPhone = (EditText)findViewById(R.id.reg_mobile);
		mPsw = (EditText)findViewById(R.id.reg_pas);
		mRePsw = (EditText)findViewById(R.id.reg_repas);
		
		mLoginBt = (Button)findViewById(R.id.regre_tologin);
		mLoginBt.setOnClickListener(listener);
		mReg = (Button)findViewById(R.id.reg_bg);
		mReg.setOnClickListener(listener);
		
		mComReg = (LinearLayout)findViewById(R.id.reg_com_user);
		mComReg.setOnClickListener(listener);
		
		reg_com_user_title = (TextView)findViewById(R.id.reg_com_user_title);
		
	}

	private View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.regre_tologin:
				
				gotoLogin();
				
				break;
			case R.id.reg_bg:
				
				if(checkInput()){
					progressDialog = ProgressDialog.show(RegistActivity.this,
							"请等待...", "正在注册，请稍后...", true, true);
					new getRegist().execute(URLHelper.mPrefix);
				}

				break;
			case R.id.reg_com_user:
				Log.e("TMG", "onclick");
				Log.e("TMG", "type = " + mClientType);
				
				if(mClientType.equals("2")){
					mClientType = "1";
					reg_com_user_title.setText("个人用户注册入口");
					headerTitle.setText("企业用户注册");
				}else{
					mClientType = "2";
					reg_com_user_title.setText("企业用户注册入口");
					headerTitle.setText("个人用户注册");
				}
				
				break;
			}

		}
	};

	private boolean checkInput() {
		boolean isNull = true;
		
		if(mMail.getText().toString().trim().equals("")){
			return false;
		}
		if(mPhone.getText().toString().trim().equals("")){
			return false;
		}
		if(mPsw.getText().toString().trim().equals("")){
			return false;
		}
		if(mRePsw.getText().toString().trim().equals("")){
			return false;
		}
		if(!(mRePsw.getText().toString()).equals(mPsw.getText().toString())){
			
			Toast toast = Toast.makeText(RegistActivity.this,
					"密码不一致", Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			return false;
		}
		
		return isNull;
	}

	
	class getRegist extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
			map.put("act", "reg");
			map.put("mobile", mPhone.getText().toString().trim());
			map.put("email", mMail.getText().toString().trim());
			map.put("password", mPsw.getText().toString().trim());
			map.put("utype", mClientType);
			Log.e("TMG", "reg type = " + mClientType);
			
			return HttpUtils.readHttp(params[0], map);// 网络操作，向服务器发送登录请求，获取返回信息

		}

		@Override
		protected void onPostExecute(String result) {
			
			progressDialog.dismiss();

			if (result == null || result.equals("")) {
				// 发送解析数据 消息
				Toast toast = Toast.makeText(RegistActivity.this,
						"失败", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				
			} else if (result.equals("timeout")) {
				// 发送解析数据 消息
				Toast toast = Toast.makeText(RegistActivity.this,
						"网络超时", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				
			} else {
				parseJson(result);
			}

		}
	}

	private void parseJson(String str) {
		Log.e("TMG", "splash result = " + str);
		try {
			String succeed = new JSONObject(str).getString("succeed");
			if (succeed.equals("000")) {
				
				Toast toast = Toast.makeText(RegistActivity.this,
						"注册成功", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				
				gotoLogin();

			} else if(succeed.equals("001")){
				Toast toast = Toast.makeText(RegistActivity.this,
						"失败", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				
			} else if(succeed.equals("002")){
				Toast toast = Toast.makeText(RegistActivity.this,
						"手机号不能为空", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				
			} else if(succeed.equals("003")){
				Toast toast = Toast.makeText(RegistActivity.this,
						"密码不能为空", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				
			} else if(succeed.equals("004")){
				Toast toast = Toast.makeText(RegistActivity.this,
						"用户类型不能为空", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				
			}else if(succeed.equals("006")){
				Toast toast = Toast.makeText(RegistActivity.this,
						"手机号已存在", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				
			}else if(succeed.equals("007")){
				Toast toast = Toast.makeText(RegistActivity.this,
						"邮箱已存在", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	private void gotoLogin(){
		
		Intent intent = new Intent(RegistActivity.this,
					LoginActivity.class);
		intent.putExtra("type", mType);
		startActivity(intent);
		
		
		ActivityTaskManager.getInstance().removeActivity("RegistActivity");
	}

}
